#!/bin/bash
# This is a ShellScript For Auto Install LNMP and Moodle on Debian/CentOS
# Add executive privilege
# chmod 755 *.sh
# Powered by hongm
# Visted http://hongm.emoodle.org
# Modify 2023-11-22
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script"
    exit 1
fi

clear
echo ""
#which CentOS Version do you want to Update?
if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
    if grep -Eqi "release 6." /etc/redhat-release; then
        ./centos6.sh
      elif grep -Eqi "release 7." /etc/redhat-release; then
        ./centos7.sh
      elif grep -Eqi "release 8." /etc/redhat-release; then
        ./centos8.sh
    fi
 elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
    Debian_OldVersion=`cat /etc/debian_version`
    echo -e " \e[0;33mYour Debian Version is \e[0;31m$Debian_OldVersion\e[0m"
    if grep -Eqi '^7.' /etc/debian_version; then
        echo -e " \e[0;33mYour Debian Version is lower,please Update.\e[0m"
        exit 1
     elif grep -Eqi '^8.' /etc/debian_version; then
        DebianSelect="2"
        echo -e " \e[0;33mYou have 10 options for Debian Update.\e[0m"
        echo -e "   \e[0;31m1\e[0m: Keep Debian 8 (jessie) with PHP 5.6 and MariaDB 10.0"
        echo -e "   \e[0;31m2\e[0m: Update to Debian 8 (jessie) with PHP 7.1 and MariaDB 10.2\e[44;37m(Default)\e[0m"
        echo -e "   \e[0;31m3\e[0m: Update to Debian 9 (stretch) with PHP 7.0 and MariaDB 10.1"
        echo -e "   \e[0;31m4\e[0m: Update to Debian 9 (stretch) with PHP 7.3 and MariaDB 10.3"
        echo -e "   \e[0;31m5\e[0m: Update to Debian 10 (buster) with PHP 7.3 and MariaDB 10.3"
        echo -e "   \e[0;31m6\e[0m: Update to Debian 10 (buster) with PHP 8.0 and MariaDB 10.5"
        echo -e "   \e[0;31m7\e[0m: Update to Debian 11 (bullseye) with PHP 7.4 and MariaDB 10.5"
        echo -e "   \e[0;31m8\e[0m: Update to Debian 11 (bullseye) with PHP 8.0 and MariaDB 10.6"
        echo -e "   \e[0;31m9\e[0m: Update to Debian 12 (bookworm) with PHP 8.2 and MariaDB 10.11"
        echo -e "   \e[0;31m0\e[0m: Update to Debian 12 (bookworm) with PHP 8.2 and MariaDB 11.1"
        echo -e "   \e[0;31me\e[0m: Exit"
        read -p "Enter your choice (1, 2, 3, 4, 5, 6, 7, 8,  9,  0 or e): " DebianSelect
        echo ""
        if [[ "${DebianSelect}" = "e" || "${DebianSelect}" = "E" ]]; then
            exit 1
         elif [ "${DebianSelect}" = "1" ]; then
            ./debian8normal.sh
         elif [[ "${DebianSelect}" = "2" || "${DebianSelect}" = "" ]]; then
            ./debian8.sh
         elif [ "${DebianSelect}" = "3" ]; then
            ./debian9normal.sh
         elif [ "${DebianSelect}" = "4" ]; then
            ./debian9.sh
         elif [ "${DebianSelect}" = "5" ]; then
            ./debian10normal.sh
         elif [ "${DebianSelect}" = "6" ]; then
            ./debian10.sh
         elif [ "${DebianSelect}" = "7" ]; then
            ./debian11normal.sh
         elif [ "${DebianSelect}" = "8" ]; then
            ./debian11.sh
         elif [ "${DebianSelect}" = "9" ]; then
            ./debian12normal.sh
         elif [ "${DebianSelect}" = "0" ]; then
            ./debian12.sh
        fi
     elif grep -Eqi '^9.' /etc/debian_version; then
        DebianSelect="2"
        echo -e " \e[0;33mYou have 8 options for Debian Update.\e[0m"
        echo -e "   \e[0;31m1\e[0m: Keep Debian 9 (stretch) with PHP 7.0 and MariaDB 10.1"
        echo -e "   \e[0;31m2\e[0m: Update to Debian 9 (stretch) with PHP 7.3 and MariaDB 10.3\e[44;37m(Default)\e[0m"
        echo -e "   \e[0;31m3\e[0m: Update to Debian 10 (buster) with PHP 7.3 and MariaDB 10.3"
        echo -e "   \e[0;31m4\e[0m: Update to Debian 10 (buster) with PHP 8.0 and MariaDB 10.5"
        echo -e "   \e[0;31m5\e[0m: Update to Debian 11 (bullseye) with PHP 7.4 and MariaDB 10.5"
        echo -e "   \e[0;31m6\e[0m: Update to Debian 11 (bullseye) with PHP 8.0 and MariaDB 10.6"
        echo -e "   \e[0;31m7\e[0m: Update to Debian 12 (bookworm) with PHP 8.2 and MariaDB 10.11"
        echo -e "   \e[0;31m8\e[0m: Update to Debian 12 (bookworm) with PHP 8.2 and MariaDB 11.1"
        echo -e "   \e[0;31me\e[0m: Exit"
        read -p "Enter your choice (1, 2, 3, 4, 5, 6, 7, 8 or e): " DebianSelect
        echo ""
        if [[ "${DebianSelect}" = "e" || "${DebianSelect}" = "E" ]]; then
            exit 1
         elif [ "${DebianSelect}" = "1" ]; then
            ./debian9normal.sh
         elif [[ "${DebianSelect}" = "2" || "${DebianSelect}" = "" ]]; then
            ./debian9.sh
         elif [ "${DebianSelect}" = "3" ]; then
            ./debian10normal.sh
         elif [ "${DebianSelect}" = "4" ]; then
            ./debian10.sh
         elif [ "${DebianSelect}" = "5" ]; then
            ./debian11normal.sh
         elif [ "${DebianSelect}" = "6" ]; then
            ./debian11.sh
         elif [ "${DebianSelect}" = "7" ]; then
            ./debian12normal.sh
         elif [ "${DebianSelect}" = "8" ]; then
            ./debian12.sh
        fi
     elif grep -Eqi '^10.' /etc/debian_version; then
        DebianSelect="2"
        echo -e " \e[0;33mYou have 6 options for Debian Update.\e[0m"
        echo -e "   \e[0;31m1\e[0m: Keep Debian 10 (buster) with PHP 7.3 and MariaDB 10.3"
        echo -e "   \e[0;31m2\e[0m: Update to Debian 10 (buster) with PHP 8.0 and MariaDB 10.5\e[44;37m(Default)\e[0m"
        echo -e "   \e[0;31m3\e[0m: Update to Debian 11 (bullseye) with PHP 7.4 and MariaDB 10.5"
        echo -e "   \e[0;31m4\e[0m: Update to Debian 11 (bullseye) with PHP 8.0 and MariaDB 10.6"
        echo -e "   \e[0;31m5\e[0m: Update to Debian 12 (bookworm) with PHP 8.2 and MariaDB 10.11"
        echo -e "   \e[0;31m6\e[0m: Update to Debian 12 (bookworm) with PHP 8.2 and MariaDB 11.1"
        echo -e "   \e[0;31me\e[0m: Exit"
        read -p "Enter your choice (1, 2, 3, 4, 5, 6 or e): " DebianSelect
        echo ""
        if [[ "${DebianSelect}" = "e" || "${DebianSelect}" = "E" ]]; then
            exit 1
         elif [ "${DebianSelect}" = "1" ]; then
            ./debian10normal.sh
         elif [[ "${DebianSelect}" = "2" || "${DebianSelect}" = "" ]]; then
            ./debian10.sh
         elif [ "${DebianSelect}" = "3" ]; then
            ./debian11normal.sh
         elif [ "${DebianSelect}" = "4" ]; then
            ./debian11.sh
         elif [ "${DebianSelect}" = "5" ]; then
            ./debian12normal.sh
         elif [ "${DebianSelect}" = "6" ]; then
            ./debian12.sh
        fi
     elif grep -Eqi '^11.' /etc/debian_version; then
        DebianSelect="2"
        echo -e " \e[0;33mYou have 4 options for Debian Update.\e[0m"
        echo -e "   \e[0;31m1\e[0m: Keep Debian 11 (bullseye) with PHP 7.4 and MariaDB 10.5"
        echo -e "   \e[0;31m2\e[0m: Update to Debian 11 (bullseye) with PHP 8.0 and MariaDB 10.7\e[44;37m(Default)\e[0m"
        echo -e "   \e[0;31m3\e[0m: Update to Debian 12 (bookworm) with PHP 8.2 and MariaDB 10.11"
        echo -e "   \e[0;31m4\e[0m: Update to Debian 12 (bookworm) with PHP 8.2 and MariaDB 11.1"
        echo -e "   \e[0;31me\e[0m: Exit"
        read -p "Enter your choice (1, 2, 3, 4 or e): " DebianSelect
        echo ""
        if [[ "${DebianSelect}" = "e" || "${DebianSelect}" = "E" ]]; then
            exit 1
         elif [ "${DebianSelect}" = "1" ]; then
            ./debian11normal.sh
         elif [[ "${DebianSelect}" = "2" || "${DebianSelect}" = "" ]]; then
            ./debian11.sh
         elif [ "${DebianSelect}" = "3" ]; then
            ./debian12normal.sh
         elif [ "${DebianSelect}" = "4" ]; then
            ./debian12.sh
        fi
     elif grep -Eqi '^12.' /etc/debian_version; then
        DebianSelect="2"
        echo -e " \e[0;33mYou have 3 options for Debian Update.\e[0m"
        echo -e "   \e[0;31m1\e[0m: Keep Debian 12 (bookworm) with PHP 8.2 and MariaDB 10.11"
        echo -e "   \e[0;31m2\e[0m: Update to Debian 12 (bookworm) with PHP 8.2 and MariaDB 11.1\e[44;37m(Default)\e[0m"
        echo -e "   \e[0;31m3\e[0m: Update to Debian 13 (sid) with PHP 8.X and MariaDB 11.X"
        echo -e "   \e[0;31me\e[0m: Exit"
        read -p "Enter your choice (1, 2, 3 or e): " DebianSelect
        echo ""
        if [[ "${DebianSelect}" = "e" || "${DebianSelect}" = "E" ]]; then
            exit 1
         elif [ "${DebianSelect}" = "1" ]; then
            ./debian12normal.sh
         elif [[ "${DebianSelect}" = "2" || "${DebianSelect}" = "" ]]; then
            ./debian12.sh
         elif [ "${DebianSelect}" = "3" ]; then
            ./debian13normal.sh
        fi
    fi
 elif uname -a | grep -Eqi "FreeBSD" ; then
    if uname -a | grep -Eqi "12."; then
        ./freebsd12.sh
     elif uname -a | grep -Eqi "11."; then
        ./freebsd11.sh
     else
        echo -e " \e[0;31mYour OS is not FreeBSD!\e[0m"
        exit 1
    fi
 else
    echo -e " \e[0;31mYour OS is not CentOS/Debian!\e[0m"
    exit 1
fi
